package com.example.pda.ui.login;

import android.app.Application;
import android.text.TextUtils;
import android.util.Patterns;

import com.example.pda.R;
import com.example.pda.common.BaseViewModel;
import com.example.pda.common.SharedUtils;
import com.example.pda.common.Utils;
import com.example.pda.data.model.Inventory;
import com.example.pda.net.ApiException;
import com.example.pda.net.ResponseCallback;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import retrofit2.http.Path;

public class LoginViewModel extends BaseViewModel {

    private final MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
    private final MutableLiveData<Boolean> loginResult = new MutableLiveData<>();
    private final MutableLiveData<Boolean> loginResult2 = new MutableLiveData<>();

    LiveData<LoginFormState> getLoginFormState() {
        return loginFormState;
    }

    public MutableLiveData<Boolean> getLoginResult() {
        return loginResult;
    }

    public MutableLiveData<Boolean> getLoginResult2() {
        return loginResult2;
    }

    public void login(String usercode, String password) {
        enqueue(api.login(usercode, password), true, new ResultCallback<String>() {
            @Override
            public void onSuccess(String s) {
                SharedUtils.setUserName(usercode);
                loginResult2.setValue(true);
            }

            @Override
            public void onError(int code, String msg) {
                loginResult2.setValue(false);
                Utils.getInstance().showToast(msg);
            }
        });
    }

    public void loginCheck(String str) {
        enqueue(api.getDjgListTest(str + "nc/pda/list/23"), true, new ResultCallback<List<Inventory>>(){

            @Override
            public void onSuccess(List<Inventory> s) {
                loginResult.setValue(true);
            }

            @Override
            public void onError(int code, String msg) {
                loginResult.setValue(false);
                Utils.getInstance().showToast(msg);
            }
        });
    }

    public void loginDataChanged(String username, String password) {
        if (TextUtils.isEmpty(username)) {
            loginFormState.setValue(new LoginFormState(R.string.login_invalid_ip, null));
        } else if (TextUtils.isEmpty(password) || password.length() < 6) {
            loginFormState.setValue(new LoginFormState(null, R.string.login_invalid_port));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }

}